package com.jyw.business.ast.task;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.google.common.collect.Lists;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.business.obstacles.constant.OrderCurrStateConstant;
import com.jyw.business.obstacles.constant.OrderSourceConstant;
import com.jyw.business.obstacles.domain.ObsOrderFault;
import com.jyw.business.obstacles.domain.ObsOrderInfo;
import com.jyw.business.obstacles.domain.dto.web.OrderAddDto;
import com.jyw.business.obstacles.service.IObsOrderFaultService;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.DictUtils;
import com.jyw.system.utils.DeptUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资产保养请求实体
 */
@Component("astMeteringTask")
@Slf4j
public class AstMeteringTask {

    @Resource
    private IObsOrderFaultService faultService;

    @Resource
    private IAstInfoService astInfoService;
    /**
     * 资产保养自动生成保养工单
     */
    @Transactional(rollbackFor = Exception.class)
    public void scan(){
        List<ObsOrderFault> faultList = faultService.lambdaQuery()
                //来源是资产保养,并且未完成的工单
                .eq(ObsOrderFault::getSource, OrderSourceConstant.AST_METERING)
                .in(ObsOrderFault::getCurrState,
                        Lists.newArrayList(
                                OrderCurrStateConstant.WAIT_DELIVERY,
                                OrderCurrStateConstant.WAIT_ACCEPT,
                                OrderCurrStateConstant.PROCESSING,
                                OrderCurrStateConstant.TERMINATED,
                                OrderCurrStateConstant.SUSPENDED,
                                OrderCurrStateConstant.WAIT_AUDIT
                        )
                )
                .list();
        //获取正在保养工单的资产
        List<Long> meteringAstList = faultList.stream().map(ObsOrderFault::getAssetsId).collect(Collectors.toList());

        LambdaQueryChainWrapper<AstInfo> astLambdaQueryWrapper = astInfoService.lambdaQuery()
                .isNotNull(AstInfo::getNextMeteringDate)
                .lt(AstInfo::getNextMeteringDate, DateUtils.getNowDate());
                if(!CollectionUtils.isEmpty(meteringAstList)){
                    astLambdaQueryWrapper
                            .notIn(AstInfo::getId, meteringAstList);
                }
        //查询需要创建工单的资产信息,排除正在保养的资产
        List<AstInfo> needMeter = astLambdaQueryWrapper
                .list();
        //根据资产信息创建工单
        needMeter.forEach(data->{
            String deptPlace = null;
            if(!ObjectUtils.isEmpty(deptPlace)){
                deptPlace = deptPlace = DeptUtils.deptIdToPathName(data.getUseDept());
            }
            OrderAddDto addDto = new OrderAddDto();
            addDto.setCreateTime(DateUtils.getNowDate());
            addDto.setSource(OrderSourceConstant.AST_METERING);
            addDto.setActualRepairPlace(Long.valueOf(DictUtils.getDictCache(SysDictTypeConstant.OBS_REPAIR_PLACE).get(0).getDictValue()));
            addDto.setFaultType(DictUtils.getDictCache(SysDictTypeConstant.OBS_FAULT_TYPE).get(0).getDictValue());
            addDto.setFaultRemark(data.getName()+",资产保养");
            addDto.setDegree(3);
            addDto.setRangeType(1);
            addDto.setResponseType(1);
            addDto.setEquipment(data.getName());
            addDto.setEquipmentAddressId(data.getUseDept());
            addDto.setEquipmentAddress(deptPlace);
            addDto.setUser("系统");
            addDto.setDeptId(data.getUseDept());
            addDto.setPlace(deptPlace);
            addDto.setAssetsId(data.getId());
            ObsOrderInfo add = faultService.add(addDto);
            log.debug("创建资产保养工单,资产:{},工单:{}",data,add);
        });
    }
}
